"""
datacenters
    name
    location

hosts
    hostname
    ip

group
    name
    hosts
    
services
    name
    hosts
"""

from sqlobject import *

class Persistent( SQLObject ):
    pass

class Host( Persistent ):
    hostname = StringCol()
    ip = StringCol()
    
    services = MultipleJoin( 'Service' )
    locations = RelatedJoin( 'Location' )

class Location( Persistent ):
    name = StringCol()
    
    hosts = RelatedJoin( 'Host' )
    
class Service( Persistent ):
    name = StringCol()
    
    host = ForeignKey( 'Host' )
    category = ForeignKey( 'Category' )
    
class Category( Persistent ):
    name = StringCol()
    
    services = MultipleJoin( 'Service' )

def init( type, location, debug = False ):
    
    con_string = type + ':/' + location
    
    if debug:
        con_string += '?debug=1'
    
    print con_string
    
    connection = dbconnection.connectionForURI( str( con_string ) )
    sqlhub.processConnection = connection

    for tab in [ Host, Location, Service, Category ]:
        tab.createTable( ifNotExists = True )